package com.yanqu.road.dao.impl.activity.zhanling;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhanling.ZhanLingState;
import com.yanqu.road.entity.activity.zhanling.ZhanLingUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/4/9 19:04
 */
public class ZhanLingUserActivityDataDaoImpl extends TempDataObjectDao<ZhanLingUserData> {
    @Override
    protected boolean addData(ZhanLingUserData data) {
        String sql = "insert into t_u_activity_zhanling(activityId,userId,triggerGiftList,triggerTime)" +
                "values(?,?,?,?)";
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        String triggerGiftListStr = "";
        if (data.getTriggerGiftList().size() > 0) {
            triggerGiftListStr = JSON.toJSONString(data.getTriggerGiftList());
        }
        para.put(i++, new DbParameter(Types.VARCHAR,  triggerGiftListStr));
        para.put(i++, new DbParameter(Types.DATE, data.getTriggerTime()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(ZhanLingUserData data) {
        String sql = "update t_u_activity_zhanling set triggerGiftList=?,triggerTime=? " +
                "where activityId=? and userId=?";
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        String triggerGiftListStr = "";
        if (data.getTriggerGiftList().size() > 0) {
            triggerGiftListStr = JSON.toJSONString(data.getTriggerGiftList());
        }
        para.put(i++, new DbParameter(Types.VARCHAR,  triggerGiftListStr));
        para.put(i++, new DbParameter(Types.DATE, data.getTriggerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ZhanLingUserData parseData(ResultSet rs) throws SQLException {
        ZhanLingUserData zhanLingUserData = new ZhanLingUserData();
        zhanLingUserData.setActivityId(rs.getInt("activityId"));
        zhanLingUserData.setUserId(rs.getLong("userId"));
        String triggerGiftStr = rs.getString("triggerGiftList");
        if (triggerGiftStr.isEmpty()) {
            zhanLingUserData.setTriggerGiftList(new ArrayList<>());
        } else {
            zhanLingUserData.setTriggerGiftList(JSON.parseObject(triggerGiftStr, new TypeReference<List<ZhanLingState>>(){}));
        }
        zhanLingUserData.setTriggerTime(rs.getDate("triggerTime"));
        return zhanLingUserData;
    }

    public Map<Integer, ZhanLingUserData> getZhanLingUserActivityData(List<Integer> activityIdList, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ZhanLingUserData> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_activity_zhanling where userId = ? and activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                ZhanLingUserData zhanLingUserData = parse(rs);
                dataMap.put(zhanLingUserData.getActivityId(), zhanLingUserData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
